//! 全局定义构建脚本
use std::{fs::File, io::Write, path::PathBuf};

use aarch64_define::{
    CurrentEL_EL2, ESR_ELx_EC_BREAKPT_CUR, ESR_ELx_EC_BREAKPT_LOW, ESR_ELx_EC_BRK64,
    ESR_ELx_EC_DABT_CUR, ESR_ELx_EC_DABT_LOW, ESR_ELx_EC_FP_ASIMD, ESR_ELx_EC_FP_EXC64,
    ESR_ELx_EC_IABT_CUR, ESR_ELx_EC_IABT_LOW, ESR_ELx_EC_PC_ALIGN, ESR_ELx_EC_SHIFT,
    ESR_ELx_EC_SP_ALIGN, ESR_ELx_EC_SVC64, ESR_ELx_EC_SYS64, ESR_ELx_EC_UNKNOWN, ESR_ELx_EC_WFx,
    PSR_MODE_EL1h, TCR_TxSZ, TCR_TxSZ_WIDTH, BOOT_CPU_MODE_EL1, BOOT_CPU_MODE_EL2, BUG_BRK_IMM,
    ENDIAN_SET_EL1, ENDIAN_SET_EL2, ID_AA64MMFR0_PARANGE_MAX, ID_AA64MMFR0_PARANGE_SHIFT,
    ID_AA64MMFR0_TGRAN_SHIFT, ID_AA64MMFR0_TGRAN_SUPPORTED, KIO_VADDR, KIO_VADDR_END, KMEM_VADDR,
    KMEM_VADDR_END, KPAGE_VADDR, KPAGE_VADDR_END, MAIR_ATTR, NO_SYSCALL, PAGE_SIZE, PGD_SHIFT,
    PGD_TYPE_TABLE, PSR_A_BIT, PSR_D_BIT, PSR_F_BIT, PSR_I_BIT, PTRS_PER_PGD, PTRS_PER_PUD,
    PUD_SHIFT, PUD_SIZE, SCTLR_EL1_RES1, SCTLR_EL1_SET, SCTLR_EL2_RES1, SWAPPER_MM_IOFLAGS,
    SWAPPER_MM_NORMALFLAGS, S_FRAME_SIZE, S_LR, S_ORIG_ADDR_LIMIT, S_ORIG_X0, S_PC, S_PSTATE, S_SP,
    S_STACKFRAME, S_SYSCALLNO, TCR_A1, TCR_ASID16, TCR_CACHE_FLAGS, TCR_IPS_SHIFT, TCR_IRGN_WBWA,
    TCR_SMP_FLAGS, TCR_T0SZ, TCR_T0SZ_OFFSET, TCR_TBI0, TCR_TG_FLAGS, THREAD_SIZE,
    TIF_NEED_RESCHED, USER_DS, _TIF_WORK_MASK,
};
use task_define::{TSK_STACK, TSK_TI_ADDR_LIMIT, TSK_TI_FLAGS, TSK_TI_PREEMPT};

fn main() {
    let out = &PathBuf::from("src/aarch64");
    let mut f = File::create(out.join("define.S")).unwrap();

    aarch64_write_define(&mut f);

    writeln!(f, "TSK_STACK = {:#x}", TSK_STACK).unwrap();
    writeln!(f, "TSK_TI_ADDR_LIMIT = {:#x}", TSK_TI_ADDR_LIMIT).unwrap();
    writeln!(f, "TSK_TI_FLAGS = {:#x}", TSK_TI_FLAGS).unwrap();
    writeln!(f, "TSK_TI_PREEMPT = {:#x}", TSK_TI_PREEMPT).unwrap();

    println!("cargo:retrun-if-changed=build.rs");
}

fn aarch64_write_define(f: &mut File) {
    writeln!(f, "KIO_VADDR = {:#x}", KIO_VADDR).unwrap();
    writeln!(f, "KIO_VADDR_END = {:#x}", KIO_VADDR_END).unwrap();
    writeln!(f, "KMEM_VADDR = {:#x}", KMEM_VADDR).unwrap();
    writeln!(f, "KMEM_VADDR_END = {:#x}", KMEM_VADDR_END).unwrap();
    writeln!(f, "KPAGE_VADDR = {:#x}", KPAGE_VADDR).unwrap();
    writeln!(f, "KPAGE_VADDR_END = {:#x}", KPAGE_VADDR_END).unwrap();

    writeln!(f, "CurrentEL_EL2 = {:#x}", CurrentEL_EL2).unwrap();

    writeln!(f, "SCTLR_EL1_RES1 = {:#x}", SCTLR_EL1_RES1).unwrap();
    writeln!(f, "SCTLR_EL2_RES1 = {:#x}", SCTLR_EL2_RES1).unwrap();

    writeln!(f, "ENDIAN_SET_EL1 = {:#x}", ENDIAN_SET_EL1).unwrap();
    writeln!(f, "ENDIAN_SET_EL2 = {:#x}", ENDIAN_SET_EL2).unwrap();

    writeln!(f, "BOOT_CPU_MODE_EL1 = {:#x}", BOOT_CPU_MODE_EL1).unwrap();
    writeln!(f, "BOOT_CPU_MODE_EL2 = {:#x}", BOOT_CPU_MODE_EL2).unwrap();

    writeln!(f, "PSR_F_BIT = {:#x}", PSR_F_BIT).unwrap();
    writeln!(f, "PSR_I_BIT = {:#x}", PSR_I_BIT).unwrap();
    writeln!(f, "PSR_A_BIT = {:#x}", PSR_A_BIT).unwrap();
    writeln!(f, "PSR_D_BIT = {:#x}", PSR_D_BIT).unwrap();

    writeln!(f, "PSR_MODE_EL1h = {:#x}", PSR_MODE_EL1h).unwrap();

    writeln!(f, "MAIR_ATTR = {:#x}", MAIR_ATTR).unwrap();

    writeln!(f, "PGD_TYPE_TABLE = {:#x}", PGD_TYPE_TABLE).unwrap();
    writeln!(f, "SWAPPER_MM_NORMALFLAGS = {:#x}", SWAPPER_MM_NORMALFLAGS).unwrap();
    writeln!(f, "SWAPPER_MM_IOFLAGS = {:#x}", SWAPPER_MM_IOFLAGS).unwrap();

    writeln!(f, "PGD_SHIFT = {:#x}", PGD_SHIFT).unwrap();
    writeln!(f, "PUD_SHIFT = {:#x}", PUD_SHIFT).unwrap();
    writeln!(f, "PTRS_PER_PGD = {:#x}", PTRS_PER_PGD).unwrap();
    writeln!(f, "PUD_SIZE = {:#x}", PUD_SIZE).unwrap();
    writeln!(f, "PTRS_PER_PUD = {:#x}", PTRS_PER_PUD).unwrap();
    writeln!(f, "PAGE_SIZE = {:#x}", PAGE_SIZE).unwrap();

    writeln!(f, "THREAD_SIZE = {:#x}", THREAD_SIZE).unwrap();

    writeln!(f, "SCTLR_EL1_SET = {:#x}", SCTLR_EL1_SET).unwrap();

    writeln!(f, "TCR_T0SZ_OFFSET = {:#x}", TCR_T0SZ_OFFSET).unwrap();
    writeln!(f, "TCR_T0SZ = {:#x}", TCR_T0SZ).unwrap();
    writeln!(f, "TCR_TxSZ = {:#x}", TCR_TxSZ).unwrap();
    writeln!(f, "TCR_TxSZ_WIDTH = {:#x}", TCR_TxSZ_WIDTH).unwrap();
    writeln!(f, "TCR_IRGN_WBWA = {:#x}", TCR_IRGN_WBWA).unwrap();

    writeln!(f, "TCR_CACHE_FLAGS = {:#x}", TCR_CACHE_FLAGS).unwrap();
    writeln!(f, "TCR_SMP_FLAGS = {:#x}", TCR_SMP_FLAGS).unwrap();
    writeln!(f, "TCR_TG_FLAGS = {:#x}", TCR_TG_FLAGS).unwrap();

    writeln!(f, "TCR_A1 = {:#x}", TCR_A1).unwrap();
    writeln!(f, "TCR_ASID16 = {:#x}", TCR_ASID16).unwrap();
    writeln!(f, "TCR_TBI0 = {:#x}", TCR_TBI0).unwrap();

    writeln!(f, "ID_AA64MMFR0_PARANGE_MAX = {:#x}", ID_AA64MMFR0_PARANGE_MAX).unwrap();
    writeln!(f, "ID_AA64MMFR0_PARANGE_SHIFT = {:#x}", ID_AA64MMFR0_PARANGE_SHIFT).unwrap();
    writeln!(f, "TCR_IPS_SHIFT = {:#x}", TCR_IPS_SHIFT).unwrap();

    writeln!(f, "ID_AA64MMFR0_TGRAN_SHIFT = {:#x}", ID_AA64MMFR0_TGRAN_SHIFT).unwrap();
    writeln!(f, "ID_AA64MMFR0_TGRAN_SUPPORTED = {:#x}", ID_AA64MMFR0_TGRAN_SUPPORTED).unwrap();

    writeln!(f, "USER_DS = {:#x}", USER_DS).unwrap();

    writeln!(f, "S_LR = {:#x}", S_LR).unwrap();
    writeln!(f, "S_SP = {:#x}", S_SP).unwrap();
    writeln!(f, "S_PC = {:#x}", S_PC).unwrap();
    writeln!(f, "S_PSTATE = {:#x}", S_PSTATE).unwrap();
    writeln!(f, "S_ORIG_X0 = {:#x}", S_ORIG_X0).unwrap();
    writeln!(f, "S_ORIG_ADDR_LIMIT = {:#x}", S_ORIG_ADDR_LIMIT).unwrap();
    writeln!(f, "S_STACKFRAME = {:#x}", S_STACKFRAME).unwrap();
    writeln!(f, "S_SYSCALLNO = {:#x}", S_SYSCALLNO).unwrap();
    writeln!(f, "S_FRAME_SIZE = {:#x}", S_FRAME_SIZE).unwrap();

    writeln!(f, "NO_SYSCALL = {:#x}", NO_SYSCALL).unwrap();

    writeln!(f, "BUG_BRK_IMM = {:#x}", BUG_BRK_IMM).unwrap();

    writeln!(f, "ESR_ELx_EC_SHIFT = {:#x}", ESR_ELx_EC_SHIFT).unwrap();
    writeln!(f, "ESR_ELx_EC_UNKNOWN = {:#x}", ESR_ELx_EC_UNKNOWN).unwrap();
    writeln!(f, "ESR_ELx_EC_WFx = {:#x}", ESR_ELx_EC_WFx).unwrap();
    writeln!(f, "ESR_ELx_EC_FP_ASIMD = {:#x}", ESR_ELx_EC_FP_ASIMD).unwrap();
    writeln!(f, "ESR_ELx_EC_SVC64 = {:#x}", ESR_ELx_EC_SVC64).unwrap();
    writeln!(f, "ESR_ELx_EC_SYS64 = {:#x}", ESR_ELx_EC_SYS64).unwrap();
    writeln!(f, "ESR_ELx_EC_SVE = {:#x}", ESR_ELx_EC_SVC64).unwrap();
    writeln!(f, "ESR_ELx_EC_IABT_LOW = {:#x}", ESR_ELx_EC_IABT_LOW).unwrap();
    writeln!(f, "ESR_ELx_EC_IABT_CUR = {:#x}", ESR_ELx_EC_IABT_CUR).unwrap();
    writeln!(f, "ESR_ELx_EC_PC_ALIGN = {:#x}", ESR_ELx_EC_PC_ALIGN).unwrap();
    writeln!(f, "ESR_ELx_EC_DABT_LOW = {:#x}", ESR_ELx_EC_DABT_LOW).unwrap();
    writeln!(f, "ESR_ELx_EC_DABT_CUR = {:#x}", ESR_ELx_EC_DABT_CUR).unwrap();
    writeln!(f, "ESR_ELx_EC_SP_ALIGN = {:#x}", ESR_ELx_EC_SP_ALIGN).unwrap();
    writeln!(f, "ESR_ELx_EC_FP_EXC64 = {:#x}", ESR_ELx_EC_FP_EXC64).unwrap();
    writeln!(f, "ESR_ELx_EC_BREAKPT_LOW = {:#x}", ESR_ELx_EC_BREAKPT_LOW).unwrap();
    writeln!(f, "ESR_ELx_EC_BREAKPT_CUR = {:#x}", ESR_ELx_EC_BREAKPT_CUR).unwrap();
    writeln!(f, "ESR_ELx_EC_BRK64 = {:#x}", ESR_ELx_EC_BRK64).unwrap();

    writeln!(f, "TIF_NEED_RESCHED = {:#x}", TIF_NEED_RESCHED).unwrap();
    writeln!(f, "_TIF_WORK_MASK = {:#x}", _TIF_WORK_MASK).unwrap();
}
